/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/* ----------------------- Platform includes --------------------------------*/
#define _MAPP_MODULE_
#include "mbapp.h"
#include "os_obj.h"                                          // RTOS object definitions
#include "common.h"

mb_baud_tab_create;
    
uint16_t DataRecevie[64];

uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;

typedef struct
{
    uint16_t reg_r[64];
    uint16_t reg_w[64];
    uint16_t pv_r[64];
    uint16_t pv_w[64];
    
    uint8_t line_w_req;
    uint8_t line_len;
    uint16_t line[64];
    
    uint8_t coil_r[16];
    uint8_t coil_w[16];
    
    uint8_t auto_cmd_len;
    uint8_t auto_cmd[64];
    
    uint16_t page_req;
    uint16_t page_set;
    
} mb_tst_type;


mb_tst_type mb_tst;

volatile uint32_t dgs_err;
volatile uint32_t dgs_ok;

void dgus_callback(uint8_t CmdSite, uint8_t ErrSt)
{
    if(ErrSt)
    {
        dgs_err++;
    }
    else
    {
       dgs_ok++; 
    }    
}

/*****************************************************************************//*!
* @brief   	Slave device handle.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/   
void modbus_task (void const *argument)
{
	mb_cmd_buff_type cmd;
	
	osDelay(INT32_MAX);
	
    mb00_Init(MB_RTU_MASTER, mb_baud_tab[MB_BAUD_38400], MB_PAR_NONE);
    mb.obj00.slave_id=1; 
    mb.obj00.rtu_rcv_end_delay_set = 5;
    mb.obj00.os_event_send = mb_os_send;
    mb.obj00.os_rtu_end_event = 0x01;
    mb.obj00.os_timeout_event = 0x02;
    
    mb_dbg_print("mbobj size %d\n", sizeof(mb.obj00));
    
    mb04_Init(MB_RTU_SLAVE, mb_baud_tab[MB_BAUD_38400], MB_PAR_NONE);
    mb.obj04.slave_id=1;  
    mb.obj04.rtu_rcv_end_delay_set = 5;
    mb.obj04.os_event_send = mb_os_send;
    mb.obj04.os_rtu_end_event = 0x04;
    mb.obj04.os_timeout_event = 0x08;
	
	cmd.device_id = 1;
	cmd.cmd = FUN_CODE_WRITE_MULTIPLE_REG;
	cmd.dat = mb_tst.pv_w;
	cmd.dat_addr = 0x40;
	cmd.amount = 8;
	cmd.call_back = dgus_callback;
    mmb_stc_cmd_req(&mb.obj00,1, &cmd);
	
	cmd.cmd = FUN_CODE_READ_REG;
	cmd.dat = mb_tst.pv_r;
    mmb_stc_cmd_req(&mb.obj00,2,&cmd);
	
	cmd.cmd = FUN_CODE_READ_INPUT;
	cmd.dat = mb_tst.reg_r;
	cmd.dat_addr = 0x10;
    mmb_stc_cmd_req(&mb.obj00,3,&cmd);
    
	cmd.cmd = FUN_CODE_W_R_MULTIPLE_REG;
	cmd.dat = mb_tst.reg_r;
	cmd.dat_addr = 0x20;
    mmb_stc_cmd_req(&mb.obj00,4,&cmd);
	
	cmd.dat = mb_tst.reg_w;
    mmb_stc_cmd_req(&mb.obj00,5,&cmd);

	cmd.cmd = FUN_CODE_WRITE_MULTIPLE_COIL;
	cmd.dat = mb_tst.reg_w;
	cmd.dat_addr = 0x20;
    mmb_stc_cmd_req(&mb.obj00,6,&cmd);
	
	cmd.cmd = FUN_CODE_READ_COILS;
	cmd.dat = mb_tst.reg_r;
    mmb_stc_cmd_req(&mb.obj00,7,&cmd);

    for(;;)
	{
        osEvent enent;
        
        enent = osSignalWait(0,ms_ticks(100));
        
        mb_poll(&mb.obj00);
        
        mb_poll(&mb.obj04);
    }
}


void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt){
    if(ErrSt)
        mbTestErrCnt++; 
    else
        mbTestOkCnt++;
}

